package epam.car.rent;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Describes an order entity
 * @author Alexander Tretyak
 *
 */
public class Order extends Management{

	private long id;
	private Client client;
	private Car car;
	private OrderState state;
	private String rejectReason;
	private Manager manager;
	private Date orderCreated;
	private Date orderExpires;
	
	/**
	 * Creates an order
	 * @param m
	 * @param client
	 * @param car
	 * @param orderCreated
	 */
	public Order(Manager m, Client client, Car car, String orderCreated){
		this.client = client;
		this.car = car;
		this.manager = m;
		this.orderCreated = setOrderCreated(orderCreated);
		this.orderExpires = setOrderExpires(orderCreated);
		this.setState(OrderState.PENDING);
		id = formId();
		//setOrderExpires(null);
	}
	/**
	 * Returns a string that contains a date in YYYY-MM-DD format when order has been created
	 */
	public String getOrderCreated() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String goc = format.format(this.orderCreated);
		return goc;
	}
	/**
	 * Sets a string that contains a date in YYYY-MM-DD format when order has been created
	 */
	public Date setOrderCreated(String orderCreated) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return (Date) format.parse(orderCreated);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * Returns a manager name that order is being managed by
	 */
	public Manager getManager() {
		return manager;
	}
	/**
	 * Sets a manager name that order is being managed by
	 */
	public void setManager(Manager manager) {
		this.manager = manager;
	}
	/**
	 * Returns a date when order expires. NOT REALISED yet
	 */
	public String getOrderExpires() {
		//SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String goe = format.format(this.orderExpires);
		return goe;
	}

	public Date setOrderExpires(String orderExpires) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			this.orderExpires = (Date) format.parse(orderExpires);
			return (Date) format.parse(orderExpires);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
		//this.orderExpires = orderExpires;
	}

	


	
	public String toString(){
		
		String result = "Order ID: " + this.id 
						+ ". " + "Order by " + client.toString() 
						+ " for car: " + car.toString() 
						+ " created " + this.getOrderCreated() + ", expires " + this.getOrderExpires() 
						+ ". State: "; 
		
		switch (getState()) {
		case APPROVED:
			result += "approved";
			break;
		case REJECTED:
			result += "rejected";
			break;
		default:
			result += "pending";
			break;
		}
		result+= ". Managed by: " + this.getManager().getName();
		return result;
	}
	
	public String toString(String param){
		String c1 = this.client.getPassportId();
		long c2 = this.car.getId();
		String result = this.id + "\t" + c1 + "\t" + c2 + "\t" + this.getOrderCreated() + "\t" + this.getState() + "\t" + this.getRejectReason() + "\t" + this.getManager() + "\t" + this.getOrderExpires() + "\t";
		return result;
	}
	
	
	public Client getClient() {
		return client;
	}

	
	public Car getCar() {
		return car;
	}

	
	public long getId() {
		return id;
	}
	
	public void setId(long id){
		this.id = id;
	}

	/**
	 * Changing a state of order to approved (see {@link OrderState})
	 */
	
	public void approveOrder(){
		this.setState(OrderState.APPROVED);
	}
	
	/**
	 * Changing a state of order to rejected (see {@link OrderState})
	 * The reason of rejection is required.
	 */
	public void rejectOrder(String reason){
		if (reason != null){
			this.setState(OrderState.REJECTED);
			this.rejectReason = reason;	
		}else{
			System.out.println("The order was not rejected, you need to indicate the reject reason.");
		}
		
	}
	
	
	public String getRejectReason(){
		return this.rejectReason;
	}

	public void setRejectReason(String rejectReason) {
		this.rejectReason = rejectReason;
	}

	public void setState(OrderState state) {
		this.state = state;
	}

	public OrderState getState() {
		return state;
	}
}
